package com.lanmaoly.cunzheng.queue.impl;

import com.lanmaoly.cunzheng.queue.Queue;

/**
 * 链式队列
 *
 * @param <T>
 * @author Administrator
 */
public class LinkedListQueue<T> implements Queue<T> {

    public static void main(String[] args) {
        LinkedListQueue<String> linkedListQueue = new LinkedListQueue<>();
        linkedListQueue.enQueue("一号选手");
        linkedListQueue.enQueue("二号选手");
        linkedListQueue.enQueue("三号选手");

        for (int i = 0; i < 10; i++) {
            if (linkedListQueue.isEmpty()) {
                break;
            }
            System.out.println("选手:" + linkedListQueue.deQueue());
        }
    }

    private class Node {

        /**
         * 节点数据
         */
        private T data;
        /**
         * 下一个节点
         */
        private Node next = null;

        Node(T data) {
            this.data = data;
        }
    }

    /**
     * // 头节点
     */
    private Node front = null;
    /**
     * // 尾节点
     */
    private Node rear = null;

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        if (front == null) {
            // 如果头节点是null，队列就是空的
            return true;
        }
        return false;
    }

    @Override
    public boolean enQueue(T t) {
        Node node = new Node(t);
        if (front == null) {
            //空队列
            front = node;
            rear = node;
        } else {
            rear.next = node;
            rear = node;
        }
        return true;
    }

    @Override
    public T deQueue() {
        if (front == null) {
            //空队列throw new Exception("空队列");
            return null;
        } else {
            T t = front.data;
            front = front.next;
//            System.out.println(front);
//            System.out.println(rear);
            return t;
        }
    }

}
